package com.gorkr.bio.file;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.Socket;

/**
 * 客户端上传任意类型的文件到服务器保存
 *
 * @author gorkr
 * @date 2022/05/31 22:12
 **/
public class Client {
    public static void main(String[] args) {
        try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\gorkr\\Pictures\\tmp\\home1.png")) {
            Socket socket = new Socket("127.0.0.1", 8888);
            // 字节输出流包装为数据输出流
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            // 发送后缀
            dataOutputStream.writeUTF(".png");
            // 文件数据发送给服务端
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer) )> 0){
                dataOutputStream.write(buffer);
            }
            dataOutputStream.flush();
            socket.shutdownOutput();  // 通知服务端数据发送结束
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
    